<?php
declare(strict_types=1);
// 返回快捷前台POS商品列表-POS销售
namespace app\pos_v1\controller;
use think\facade\Db; //门面模式
use app\pos_v1\model\GoodsPosList as mysql; //导入模型
class GoodsPosList
{
 
    // 返回快捷前台POS商品列表-POS销售
    public function GoodsPosList()
    {
        // 请求方式验证
        if (!request()->isPost()) {
            $apidb = ['code' => 201, 'msg' => "请求有误-不是Post方式!",];
            return json($apidb);
        }
        ;
        $query = request()->param(); //请求参数
        $limit = request()->param('limit', 10, 'trim'); //每页行数
        $current = request()->param('current', 1, 'trim'); //页码
        $sqldb = mysql::where([
            ["pos_show", "=", "1"],
            // ["cls_id", "IN", [1, 150, 129, 128, 123, 124, 154, 120, 121, 122]],
            // ["unit_pack", "=", "B"], //只查小包单位
        ])
            // ->order($SearchaSortField, $SearchaSort)
            ->paginate([ //[文档]https://www.kancloud.cn/manual/thinkphp6_0/1037638
                'list_rows' => $limit, // 每页数量
                'page' => $current, // 当前页
                // 'query' => $query,//url额外参数
            ]);
        //设置允许输出字段
        $sqldb = $sqldb->visible([
            // "id", //主键
            // "dbid",//数据编号.唯一',
            // "barcode",//主条码',
            // "unit_pack",//是否大单位_A是B否',
            // "unit_goods_dbid",//大单位父商品数据编号(基本单位等于自己)',
            "db_json",//静态JSON',
        ]);
        $sqldb2 = $sqldb->toArray();
        for ($i = 0; $i < count($sqldb2["data"]); $i++) {
            $sqldb2["data"][$i] = json_decode($sqldb2["data"][$i]["db_json"], true);//转数组
            unset($sqldb2["data"][$i]["pos_show"]);
            unset($sqldb2["data"][$i]["code_x"]);
        }
        $help = [
            "db" => [
                "total" => "数据总行数",
                "per_page" => "每页数量",
                "current_page" => "当前页码",
                "last_page" => "最后一页/总页数",
                "data=商品数组数据集-实际有些不返回" => [
                    [
                        "dbid" => "数据编号.唯一",
                        "unit" => "单位",
                        "unit_goods_dbid" => "大单位父商品数据编号(基本单位等于自己)",
                        "unit_pack" => "是否大单位_A是B否",
                        "name" => "商品名称",
                        "barcode" => "主条码",
                        "goods_no" => "货号",
                        "auto_number" => "自动编号",
                        "custom_code" => "自编码",
                        "prc_in" => "进价",
                        "prc_vip" => "会员价",
                        "prc_sale" => "零售价",
                        "code_x" => "SQL模糊查询码",
                        "pos_show" => "前台快捷商品显示"
                    ]
                ]
            ]
        ];
        $apidb = [
            'code' => 200, //状态码
            'msg' => "查询成功！", //状态信息
            'time' => time(),
            'query' => $query, //请求参数
            // 'db' => $goods_db,
            'db' => $sqldb2,
            // 'aaaaaaaa' => db::getLastSql(), //输出上次执行的sql语句/也可以使用fetchSql方法直接返回当前的查询SQL而不执行，例如：echo User::fetchSql()->find(1);
            'api' => ["name" => "返回快捷前台POS商品列表-POS销售", "help" => $help], //API信息
        ];
        return (json($apidb));
    }
}
